package org.qq.clientthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * 
 * project_name:QQ
 * package_name:org.link.clientthread
 * file_name:RegisterUserThread.java
 * @author Link
 * create_date:2015年8月29日 下午12:56:32
 * modified_date:
 * description: 用户注册线程
 *
 */

public class ClientRegistThread extends Thread{
	private Socket s;					//套接字
	private BufferedReader br = null;
	private PrintStream ps = null;
	private String reply;				//服务器端答复语
	private JFrame jframe;
	
	public ClientRegistThread() {//无參构造方法
		super();
	}
	
	public ClientRegistThread(Socket s,JFrame jframe) {//全參构造方法
		super();
		this.s = s;
		this.jframe = jframe;
		try {
			br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		
		while(true){
			try {
				reply = br.readLine();
			} catch (IOException e) {
				return;
			}
			if(reply.endsWith("true----regist")){
				int num = JOptionPane.showConfirmDialog(jframe, "注册成功!","注册提示",JOptionPane.YES_OPTION);
				if(num == JOptionPane.YES_OPTION){
					jframe.dispose();//关闭注册窗口
				}
			}else if(reply.endsWith("false----regist")){
				JOptionPane.showMessageDialog(null, "账号已存在,请重新输入!","错误提示",JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	/**
	 * 
	 * description: 发送消息到服务端
	 * 2015年8月31日 下午3:11:32
	 * @author Link
	 * void
	 *
	 */
	public synchronized void sendMessageToServer(String str1){
		try {
			ps = new PrintStream(s.getOutputStream());
			ps.println(str1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}
